define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'campaign_data/index' + location.search,
                    add_url: 'campaign_data/add',
                    edit_url: 'campaign_data/edit',
                    del_url: 'campaign_data/del',
                    multi_url: 'campaign_data/multi',
                    table: 'campaign_data',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'site_id', visible: false, title: __('Site_id')},
                        {field: 'user.sitename', title: __('User.sitename')},
                        {field: 'name', title: __('Name')},
                        {field: 'age', title: __('Age')},
                        {field: 'nation', title: __('Nation')},
                        {field: 'politic', title: __('Politic'), searchList: {"1":__('Politic 1'),"2":__('Politic 2'),"3":__('Politic 3')}, formatter: Table.api.formatter.normal},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'job', title: __('Job')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        select: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'campaign_data/index' + location.search,
                    add_url: 'campaign_data/add',
                    edit_url: 'campaign_data/edit',
                    del_url: 'campaign_data/del',
                    multi_url: 'campaign_data/multi',
                    table: 'campaign_data',
                },
                queryParams: function (params) { //自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                    var op = params.op ? JSON.parse(params.op) : {};  //并将搜索过滤器 转为对象方便我们追加条件
                    filter.status= '1';     //将需要的参数group_id 加入到搜索条件中去 要求url传递的参数必须为group_id=XX;可以用 Fast.api.query("group_id")获取到！
                    op.status= "=";  //group_id的操作方法的为 找到相等的
                    params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
                    params.op = JSON.stringify(op);
                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify({url: 'login'});
                    //params.op = JSON.stringify({url: 'like'});
                    return params;
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'site_id', visible: false, title: __('Site_id')},
                        {field: 'user.sitename', title: __('User.sitename')},
                        {field: 'name', title: __('Name')},
                        {field: 'age', title: __('Age')},
                        {field: 'nation', title: __('Nation')},
                        {field: 'politic', title: __('Politic'), searchList: {"1":__('Politic 1'),"2":__('Politic 2'),"3":__('Politic 3')}, formatter: Table.api.formatter.normal},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'job', title: __('Job')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", ".btn-addselect", function () {
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                console.log(ids);
                Fast.api.ajax({
                    url:'campaign_data/add_select',
                    data:{id:ids.join(","),vote_id:Fast.api.query("vote_id")}
                }, function(data, ret){
                    setTimeout(function () {
                        Fast.api.close(data);
                    },1500);
                    //成功的回调
                }, function(data, ret){
                    //失败的回调
                    return false;
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});